﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace TestProject.Assessment
{
    public class CassiniLuncher
    {
        private static void GetDevelopmentServerVPathAndPortFromProjectFile(
            string csprojFileName,
            out string developmentServerVPath,
            out int developmentServerPort)
        {
            var doc = new XPathDocument(csprojFileName);
            XPathNavigator navigator = doc.CreateNavigator();
            var manager = new XmlNamespaceManager(navigator.NameTable);
            manager.AddNamespace("msbuild",
                                 "http://schemas.microsoft.com/developer/msbuild/2003");
            const string xpath = "/msbuild:Project/msbuild:ProjectExtensions/"
                                 + "msbuild:VisualStudio/msbuild:FlavorProperties/"
                                 + "msbuild:WebProjectProperties";
            XPathNavigator webProjectPropertiesNode =
                navigator.SelectSingleNode(xpath, manager);
            XPathNavigator developmentServerPortNode =
                webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerPort",
                                                          manager);
            XPathNavigator developmentServerVPathNode =
                webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerVPath",
                                                          manager);
            developmentServerPort = developmentServerPortNode.ValueAsInt;
            developmentServerVPath = developmentServerVPathNode.Value;
        }

        private static string GetCommonProgramFilesPath()
        {
            string commonProgramFiles =
                Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");
            if (string.IsNullOrEmpty(commonProgramFiles))
            {
                commonProgramFiles =
                    Environment.GetEnvironmentVariable("CommonProgramFiles");
            }
            if (string.IsNullOrEmpty(commonProgramFiles))
            {
                commonProgramFiles =
                    Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
            }
            return commonProgramFiles;
        }

        public static Process PrepareCassiniProcess(int developmentServerPort,
                                                     string projectPhysicalPath,
                                                     string developmentServerVPath)
        {
            string commonProgramFiles = GetCommonProgramFilesPath();
            string cassiniPath = Path.Combine(commonProgramFiles,
                                              @"Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe");
            string cassiniArgs = string.Format(
                CultureInfo.InvariantCulture,
                "/port:{0} /nodirlist /path:\"{1}\" /vpath:\"{2}\"",
                developmentServerPort, projectPhysicalPath, developmentServerVPath);
            var cassiniProcess = new Process {StartInfo = {FileName = cassiniPath, Arguments = cassiniArgs}};
            return cassiniProcess;
        }
    }
}
